package com.freeLearning.boot.course.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.freeLearning.boot.common.annotation.Log;
import com.freeLearning.boot.common.enums.LogModuleEnum;
import com.freeLearning.boot.common.result.Result;
import com.freeLearning.boot.course.model.VO.CourseChaptersSortVO;
import com.freeLearning.boot.course.model.VO.CourseInfoPageVO;
import com.freeLearning.boot.course.model.entity.CourseChapters;
import com.freeLearning.boot.course.model.query.CourseChaptersPageQuery;
import com.freeLearning.boot.course.model.query.CourseInfoPageQuery;
import com.freeLearning.boot.course.service.CourseChaptersService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "19.课程信息分集接口")
@RequestMapping("/api/v1/course/chapters")
@RestController
@RequiredArgsConstructor
public class CourseChaptersController {
    private final CourseChaptersService courseChaptersService;

    @PostMapping("/page")
    @Operation(summary = "分页查询分集")
    @Log(value = "分页查询课程", module = LogModuleEnum.COURSEINFO)
    public Result<IPage<CourseChapters>> page(@RequestBody CourseChaptersPageQuery courseChaptersPageQuery){
        IPage<CourseChapters> result =  courseChaptersService.pageQuery(courseChaptersPageQuery);
        return Result.success(result);
    }

    @PostMapping("/add")
    @Operation(summary = "新增分集")
    @Log(value = "新增课程分集", module = LogModuleEnum.COURSEINFO)
    public Result<CourseChapters> add(@RequestBody CourseChapters courseChapters){

       courseChaptersService.save(courseChapters);
        return Result.success(courseChapters);
    }

    @PostMapping("/update")
    @Operation(summary = "修改分集")
    @Log(value = "修改课程分集", module = LogModuleEnum.COURSEINFO)
    public Result<Boolean> update(@RequestBody CourseChapters courseChapters){
        boolean result = courseChaptersService.updateById(courseChapters);
        return Result.success(result);
    }
    @PostMapping("/delete/{id}")
    @Operation(summary = "删除分集")
    @Log(value = "删除课程分集", module = LogModuleEnum.COURSEINFO)
    public Result<Boolean> delete(@PathVariable Long id){
        boolean result = courseChaptersService.removeById(id);
        return Result.success(result);
    }

    @GetMapping("/sortChaptersList/{id}")
    @Operation(summary = "查询课程排序列表")
    public Result<List<CourseChaptersSortVO>> getSortChaptersList(@PathVariable Long id){
        List<CourseChapters> list = courseChaptersService.list(new QueryWrapper<CourseChapters>().eq("course_id", id).orderByAsc("chapter_order"));
        List<CourseChaptersSortVO> courseChaptersSortVOS = BeanUtil.copyToList(list, CourseChaptersSortVO.class);
        return Result.success(courseChaptersSortVOS);
    }

    @PostMapping("/sortChapters")
    @Operation(summary = "修改课程排序")
    public Result<Boolean> sortChapters(@RequestBody List<CourseChaptersSortVO> courseChaptersSortVOS){
        boolean result = courseChaptersService.sortChapters(courseChaptersSortVOS);
        return Result.success(result);
    }

}
